package com.ehomepay.xyxeample.common.base;

import com.ehomepay.xyxeample.common.entity.ResultVo;
import com.ehomepay.xyxeample.common.enums.RetCodeEnum;
import com.ehomepay.xyxeample.common.redis.config.RedisConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Administrator on 2018/5/8.
 */
public abstract class BaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     *
     * @Description: 返回封装,code=400
     * @param info
     * @return ResultVo
     */
    protected ResultVo warn(String info) {
        ResultVo vo = new ResultVo();
        vo.setCode(RetCodeEnum.ERROR_PARAM.getKey());
        vo.setInfo(info);
        return vo;
    }

    /**
     *
     * @Description: 返回封装,code=500
     * @param info
     * @return ResultVo
     */
    protected ResultVo error(String info) {
        ResultVo vo = new ResultVo();
        vo.setCode(RetCodeEnum.ERROR_SERVER.getKey());
        vo.setInfo(info);
        return vo;
    }

    /**
     * @Description:返回封装，code=500支持自定义res信息
     * @param info
     * @param res
     * @return ResultVo
     */
    protected ResultVo error(String info, Object res) {
        ResultVo vo = new ResultVo();
        vo.setCode(RetCodeEnum.ERROR_SERVER.getKey());
        vo.setInfo(info);
        vo.setData(res);
        return vo;
    }

    /**
     * @Description: 返回封装，code=0
     * @param info
     * @param res
     * @return    设定文件
     */
    protected ResultVo success(Object res) {
        return success(null, res);
    }

    /**
     * @Description:自定义提示信息方法
     * @param code 返回码{@link ResultCode}
     * @param info 页面提示信息
     * @param res 数据字段          可以为空
     * @return ResultVo
     */
    protected ResultVo message(String code, String info, Object res) {
        ResultVo vo = new ResultVo();
        vo.setCode(code);
        vo.setInfo(info);
        if(null != res){
            vo.setData(res);
        }
        return vo;
    }

    protected ResultVo message(String code,String info){
        return this.message(code, info,null);
    }

    /**
     * @Description: 返回封装，code=200
     * @param info
     * @param res
     * @return    设定文件
     */
    public ResultVo success(String info, Object res) {
        ResultVo vo = new ResultVo();
        vo.setCode(RetCodeEnum.SUCCESS.getKey());
        if (info == null) {
            vo.setInfo("success");
        } else {
            vo.setInfo(info);
        }
        if(null != res){
            vo.setData(res);
        }
        return vo;
    }
}
